import java.util.*;

public class J102402 {
    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> list = new ArrayList<>();
        //分别记录s和p中每个字符的个数
        int[] hashs = new int[26];
        int[] hashp = new int[26];
        int slen = s.length();
        int plen = p.length();
        for (int i = 0; i < p.length(); i++) {
            hashp[p.charAt(i) - 'a']++;
        }
        //创建有效字符(即区间中s中某一字符数量少于p中该字符的这一字符)变量
        int sk = 0;
        for (int left = 0, right = 0; right < slen; right++) {
            char chs1 = s.charAt(right);
            //如果目前区间中该字符个数小于p中该字符个数则有效字符++
            if (++hashs[chs1 - 'a'] <= hashp[chs1 - 'a']) {
                sk++;
            }
            //当有效字符个数与p.length相等时
            if (sk == plen) {
                //当目前区间长度大于p.length时
                while (right - left + 1 > plen) {
                    char chs2 = s.charAt(left++);
                    if (hashs[chs2 - 'a']-- <= hashp[chs2 - 'a']) {
                        sk--;
                    }
                }
                //如果区间长度等于p.length时有效字符个数仍然相等，则目前区间为有效区间
                if (sk == plen) {
                    list.add(left);
                }
            }
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(findAnagrams("cbaebabacd", "abc"));
    }
}
